#include "RtcInitHelper.h"

#include <time.h>
#include <hc32_ll_utility.h>

//RTC Config
void App_RTCCfg(void)
{
    stc_rtc_init_t stcRtcInit;
    stc_rtc_alarm_t stcRtcAlarm;

    /* Reset RTC counter */
    if (LL_ERR_TIMEOUT != RTC_DeInit()) {
        /* Configure structure initialization */
        (void)RTC_StructInit(&stcRtcInit);

        /* Configuration RTC structure */
        stcRtcInit.u8ClockSrc = RTC_CLK_SRC_XTAL32;
        stcRtcInit.u8HourFormat = RTC_HOUR_FMT_24H;
        stcRtcInit.u8IntPeriod = RTC_INT_PERIOD_PER_HOUR;
        stcRtcInit.u8ClockCompen = RTC_CLK_COMPEN_DISABLE;
        (void)RTC_Init(&stcRtcInit);

        /* Configuration alarm clock time */
        stcRtcAlarm.u8AlarmHour = 0x12U;
        stcRtcAlarm.u8AlarmMinute = 0x0U;
        stcRtcAlarm.u8AlarmWeekday = RTC_ALARM_WEEKDAY_SUNDAY | RTC_ALARM_WEEKDAY_MONDAY | RTC_ALARM_WEEKDAY_TUESDAY | RTC_ALARM_WEEKDAY_WEDNESDAY | RTC_ALARM_WEEKDAY_THURSDAY | RTC_ALARM_WEEKDAY_FRIDAY | RTC_ALARM_WEEKDAY_SATURDAY;
        stcRtcAlarm.u8AlarmAmPm = RTC_HOUR_12H_PM;
        (void)RTC_SetAlarm(RTC_DATA_FMT_BCD, &stcRtcAlarm);
        RTC_AlarmCmd(ENABLE);

        /* Enable RTC interrupt */
        RTC_IntCmd(RTC_INT_PERIOD | RTC_INT_ALARM, ENABLE);
        /* Startup RTC count */
        RTC_Cmd(ENABLE);
    }
}

//_ARMABI time_t time(time_t * timer)
//{
//	uint32_t nTime=RTC_ReadTimeCounter(&hrtc);
//	if(timer!=NULL)
//	{
//		*timer=nTime;
//	}
//	return nTime;
//}

_ARMABI clock_t clock(void)
{
	return SysTick_GetTick();
}
